package cn.ai.shoes.client.controller;

import cn.ai.shoes.client.entity.ComfyuiLog;
import cn.ai.shoes.client.entity.vo.ComfyuiLogPageVo;
import cn.ai.shoes.client.service.ComfyuiLogService;
import cn.ai.shoes.common.exception.constant.GlobalErrorCodeConstants;
import cn.ai.shoes.common.pojo.entity.CommonResult;
import cn.ai.shoes.core.pojo.entity.MyUser;
import cn.ai.shoes.core.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jodd.util.StringUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comfyui")
@Tag(name = "ComfyUI调用日志管理")
public class ComfyuiLogController {

    @Resource
    private ComfyuiLogService comfyuiLogService;
    @Resource
    private UserService userService;

    @GetMapping("/logs")
    @Operation(summary = "分页查询ComfyUI调用日志")
    public CommonResult<?> pageComfyuiLogs(ComfyuiLogPageVo pageVo) {
        MyUser user =null;
        IPage<ComfyuiLog> page =null;
        //查询用户信息
        if (StringUtil.isNotEmpty(pageVo.getUid())) {
            user = userService.selectOneByUid(pageVo.getUid());
        }else{
            return CommonResult.error(GlobalErrorCodeConstants.UID_NOT_EXIST);
        }
        //判断用户不存在
        if (user == null) {
            return CommonResult.error(GlobalErrorCodeConstants.USER_NOT_FOUND);
        }
        //判断用户不是管理员
        if (!user.isAdmin()) {
            //设置用户名
            if (user.getUsername()!= null) {
                pageVo.setUsername(user.getUsername());
            } else {
                return CommonResult.error(GlobalErrorCodeConstants.USER_NOT_EXIST);
            }
        }
        page = comfyuiLogService.pageComfyuiLog(pageVo);
        return CommonResult.success(page);
    }
}
